package top.tagao.ref_02_classUse;

import org.junit.Test;
import top.tagao.Person;

import java.lang.annotation.ElementType;

/**
 * @author fu-xiao-liu
 * @Date 2022/3/24 16:03
 * 获取Class的实例的方式（前三种方式需要掌握）
 */
public class ClassUse {
    @Test
    public void test01() throws ClassNotFoundException {
        //方式一：调用运行时类的属性：.class
        Class clazz1 = Person.class;
        System.out.println(clazz1);
        //方式二：通过运行时类的对象,调用getClass()
        Person p1 = new Person();
        Class clazz2 = p1.getClass();
        System.out.println(clazz2);

        //方式三：调用Class的静态方法：forName(String classPath)
        Class clazz3 = Class.forName("top.tagao.Person");
        System.out.println(clazz3);

        //方式四：使用类的加载器：ClassLoader  (了解)
        ClassLoader classLoader = ClassUse.class.getClassLoader();
        Class clazz4 = classLoader.loadClass("top.tagao.Person");
        System.out.println(clazz4);

        //内置基本数据类型的包装类型可以直接用类名 .Type
        Class clazz5 = Integer.TYPE;
        System.out.println(clazz5);
    }


    @Test
    public void test4(){
        Class c1 = Object.class;
        Class c2 = Comparable.class;
        Class c3 = String[].class;
        Class c4 = int[][].class;
        Class c5 = ElementType.class;
        Class c6 = Override.class;
        Class c7 = int.class;
        Class c8 = void.class;
        Class c9 = Class.class;
        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
        System.out.println(c4);
        System.out.println(c5);
        System.out.println(c6);
        System.out.println(c7);
        System.out.println(c8);
        System.out.println(c9);

        int[] a = new int[10];
        int[] b = new int[100];
        // 只要数组的元素类型与维度一样，就是同一个Class
        System.out.println(a.getClass().hashCode());
        System.out.println(b.getClass().hashCode());

    }
}
